package com.h3bpm.kumhosunny.knowledge.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Data.Metadata.EnumerableMetadata;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @zpf
 * @author
 * @date2019/8/22
 */
@Controller
@RequestMapping("/Portal/kumhosunny/viewdata")
public class KnowledegeViewDataController extends ControllerBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBase.class);

    @Override
    public String getFunctionCode() {
        return null;
    }



    /****
     * 功能描述: 知识主数据字段映射
     *
     * @Param: [instanceId]
     * @Return: void
     * @Author: zhangpf
     * @Date: 2019/8/20 9:09
     */
    @RequestMapping(value = "/knowledgeDataMapping",  method = {RequestMethod.POST,RequestMethod.GET},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public Map<String,List<Map<String,String>>> knowledgeDataMapping( ){

            String sql = " select  a.paramName,a.paramCode,a.controlTypes  from i_knowledgeMapping a  order by a.CreatedTime desc";

        String sqlChild = " select  a.param,a.type   from i_materialTraitParam a  order by a.CreatedTime desc";

        try {

            DataTable dataTable = SqlUtil.doQuery(sql);

            DataTable dataTableChild = SqlUtil.doQuery(sqlChild);

            List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();

            for(DataRow a : dataTable.getEntityRows()){

                  Map<String,String> map= new HashMap<String,String>(3);
                  map.put("paramName",a.getString("paramName"));
                  map.put("paramCode",a.getString("paramCode"));
                  map.put("controlTypes",a.getString("controlTypes"));
                  resultList.add(map);
            }

            List<Map<String,String>> resultListChild = new ArrayList<Map<String,String>>();

            for(DataRow a : dataTableChild.getEntityRows()){

                Map<String,String> map= new HashMap<String,String>(3);
                map.put("paramName",a.getString("param"));
                map.put("controlTypes",a.getString("type"));
                resultListChild.add(map);
            }
            Map<String,List<Map<String,String>>> resultMap = new HashMap<>(3);
            resultMap.put("listParent",resultList);
            resultMap.put("listChild",resultListChild);

            return resultMap ;

        } catch (Exception e) {
            e.printStackTrace();

        }
           return null;
    }


    @RequestMapping(value = "/getDataRepository",  method = {RequestMethod.POST,RequestMethod.GET},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public Map<String,List<Map<String,String>>>  getDataRepository(){
        Map<String,List< Map<String,String>>> result = new HashMap<>(3);
         try {
        List<EnumerableMetadata>  partsDataList = getEngine().getMetadataRepository().GetByCategory("知识-零部件");
        List<EnumerableMetadata>  moldingMethodDataList = getEngine().getMetadataRepository().GetByCategory("知识-成型方式");
        List<EnumerableMetadata>  baseMaterialaDataList = getEngine().getMetadataRepository().GetByCategory("知识-树脂基材A");
        List<EnumerableMetadata>  baseMaterialbDataList = getEngine().getMetadataRepository().GetByCategory("知识-树脂基材B");
        List<EnumerableMetadata>  industryDataList = getEngine().getMetadataRepository().GetByCategory("知识-行业");
        List<EnumerableMetadata>      productDataList = getEngine().getMetadataRepository().GetByCategory("知识-产品");
        List<EnumerableMetadata>      processingDataList = getEngine().getMetadataRepository().GetByCategory("知识-后加工");
        List< Map<String,String>> partsList = new ArrayList<>();
        List<Map<String,String>> moldingMethodList = new ArrayList<>();
        List<Map<String,String>> baseMaterialaList = new ArrayList<>();
        List<Map<String,String>> baseMaterialbList = new ArrayList<>();
        List<Map<String,String>> industryList = new ArrayList<>();
        List< Map<String,String>> productList = new ArrayList<>();
        List<Map<String,String> > processingList = new ArrayList<>();
         result = putDataMap(  result,   partsList,  partsDataList,  "spareParts");
         result = putDataMap(  result,   moldingMethodList,  moldingMethodDataList,  "formingMethod");
         result = putDataMap(  result,   baseMaterialaList,  baseMaterialaDataList,  "resinSubstrateA");
         result = putDataMap(  result,   baseMaterialbList,  baseMaterialbDataList,  "resinSubstrateB");
         result = putDataMap(  result,   industryList,  industryDataList,  "trade");
         result = putDataMap(  result,   productList,  productDataList,  "product");
         result = putDataMap(  result,   processingList,  processingDataList,  "postProcessing");
        return result;
        } catch (Exception e) {

            result.put("error",null);
            e.printStackTrace();
        }
      return result;
    }


    public Map<String,List< Map<String,String>>> putDataMap(Map<String,List< Map<String,String>>> result,  List< Map<String,String>> paramList,List<EnumerableMetadata> data,String codeName){

        for(EnumerableMetadata a : data){
            Map<String,String> dataMap = new HashMap<>(3);
            dataMap.put("code",a.getEnumValue());
            dataMap.put("name",a.getEnumValue());
            paramList.add(dataMap);
        }
        result.put(codeName,paramList);
        return result;
    }


@RequestMapping(value = "/getKnowledegeSql",  method = {RequestMethod.POST,RequestMethod.GET},produces = "text/html;charset=UTF-8")
@ResponseBody
public Map<String,String> getKnowledegeSql(){

    BASE64Encoder encoder = new BASE64Encoder();

    String baseSql= "SELECT distinct a.documentName,a.trade,a.product,a.spareParts,a.formingMethod,a.brandName,b.FinishTime,b.OriginatorName,b.Originator,b.ObjectID as instanceId from i_knowledge a INNER JOIN ot_instancecontext b on a.ObjectID = b.BizObjectId left join i_materialtrait c on c.ParentObjectID=a.ObjectID  where 1=1 ";

    String baseSqlEncoder = encoder.encode(baseSql.getBytes());

    Map<String,String> sqlMap = new HashMap<>(3);

    sqlMap.put("baseSqlEncoder",baseSqlEncoder);

     return sqlMap;
}

}
